package com.itboye.ihomebank.home;

import java.util.ArrayList;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;

import com.itboye.ihomebank.R;
import com.itboye.ihomebank.base.BaseOtherActivity;
import com.itboye.ihomebank.bean.FeiYongFenTanChooseBean;

public class ActivityFeiYongFenTan extends BaseOtherActivity {
	GridView gridView;
	TextView add_shap_title_tv, save;
	ArrayList<FeiYongFenTanChooseBean> arrayList = new ArrayList<FeiYongFenTanChooseBean>();
	ArrayList<Drawable> arrayListImgSelected = new ArrayList<>();
	ArrayList<Drawable> arrayListImgUnSelected = new ArrayList<>();
	private MyAadpter adapter;
	StringBuffer stringBufferSelected = new StringBuffer();
	StringBuffer stringBufferUnSelected = new StringBuffer();

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.close_icon:
			finish();
			break;
		case R.id.save:
			stringBufferSelected = new StringBuffer();
			stringBufferUnSelected = new StringBuffer();
			for (int i = 0; i < arrayList.size(); i++) {
				if (arrayList.get(i).isSelected()) {
					stringBufferSelected.append(arrayList.get(i).getName()
							+ "、");
				} else {
					stringBufferUnSelected.append(arrayList.get(i).getName()
							+ "、");
				}
			}
			Intent intent = new Intent();
			intent.putExtra(
					"select",
					stringBufferSelected.length() > 0 ? stringBufferSelected
							.toString().substring(0,
									stringBufferSelected.length() - 1) : "");
			intent.putExtra(
					"unselect",
					stringBufferUnSelected.length() > 0 ? stringBufferUnSelected
							.toString().substring(0,
									stringBufferUnSelected.length() - 1) : "");
			setResult(104, intent);
			finish();
			break;

		default:
			break;
		}
	}

	@Override
	protected int layout_id() {
		// TODO Auto-generated method stub
		return R.layout.activity_feiyongfentanxuanze;
	}

	@Override
	protected void onInitController() {
		// TODO Auto-generated method stub

	}

	@Override
	protected void onInit() {
		// TODO Auto-generated method stub
		add_shap_title_tv.setText("费用分摊选择");
		save.setVisibility(View.VISIBLE);
		save.setTextSize(15);
		arrayListImgSelected.add(getResources().getDrawable(
				R.drawable.wuyefei_selected));
		arrayListImgUnSelected.add(getResources().getDrawable(
				R.drawable.wuyefei));
		arrayListImgSelected.add(getResources().getDrawable(
				R.drawable.wifi_selected));
		arrayListImgUnSelected.add(getResources().getDrawable(R.drawable.wifi));
		arrayListImgSelected.add(getResources().getDrawable(
				R.drawable.weishengfei_selected));
		arrayListImgUnSelected.add(getResources().getDrawable(
				R.drawable.weishengfei));
		arrayListImgSelected.add(getResources().getDrawable(
				R.drawable.dianfei_selected));
		arrayListImgUnSelected.add(getResources().getDrawable(
				R.drawable.dianqifei));
		arrayListImgSelected.add(getResources().getDrawable(
				R.drawable.shuifei_selected));
		arrayListImgUnSelected.add(getResources().getDrawable(
				R.drawable.shuifei));
		arrayListImgSelected.add(getResources().getDrawable(
				R.drawable.ranqifei_selected));
		arrayListImgUnSelected.add(getResources().getDrawable(
				R.drawable.ranqifei));
		FeiYongFenTanChooseBean bean = new FeiYongFenTanChooseBean();
		bean.setName("物业费");
		arrayList.add(bean);
		bean = new FeiYongFenTanChooseBean();
		bean.setName("wifi");
		arrayList.add(bean);
		bean = new FeiYongFenTanChooseBean();
		bean.setName("卫生费");
		arrayList.add(bean);
		bean = new FeiYongFenTanChooseBean();
		bean.setName("电费");
		arrayList.add(bean);
		bean = new FeiYongFenTanChooseBean();
		bean.setName("水费");
		arrayList.add(bean);
		bean = new FeiYongFenTanChooseBean();
		bean.setName("燃气费");
		arrayList.add(bean);
		adapter = new MyAadpter();
		gridView.setAdapter(adapter);
		gridView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				// TODO Auto-generated method stub
				boolean isSelected = arrayList.get(position).isSelected();
				arrayList.get(position).setSelected(!isSelected);
				adapter.notifyDataSetChanged();
			}
		});
	}

	@Override
	protected void onReadIntent(Bundle bundle) {
		// TODO Auto-generated method stub

	}

	class MyAadpter extends BaseAdapter {

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return arrayList.size();
		}

		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return arrayList.get(position);
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return position;
		}

		@SuppressLint("ResourceAsColor")
		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			// TODO Auto-generated method stub
			convertView = LayoutInflater.from(ActivityFeiYongFenTan.this)
					.inflate(R.layout.item_feiyongfentan, null);
			TextView textView = (TextView) convertView.findViewById(R.id.txt);
			ImageView image = (ImageView) convertView.findViewById(R.id.img);
			if (arrayList.get(position).isSelected()) {
				image.setBackgroundDrawable(arrayListImgSelected.get(position));
				textView.setTextColor(Color.parseColor("#6183EF"));
			} else {
				image.setBackgroundDrawable(arrayListImgUnSelected
						.get(position));
				textView.setTextColor(Color.parseColor("#000000"));
			}
			textView.setText(arrayList.get(position).getName());
			return convertView;
		}

	}

}
